/*****************************************************************************\

File:   otkFamilyMemberExample.java

Date          Build     Author        Version   Comments
08-Aug-12    P-20-11    aphatak       $$1       Created
21-Aug-12    P-20-13    aphatak       $$2       Updated
20-mar-15 P-30-05 gshmelev $$3  called GetCurrentSession


\****************************************************************************/

/*
  Example based on test\java\java\jlinkexamples\com\ptc\jlinkexamples\pfcFamilyMemberExamples.java
  This example identifies hole features in the current model.  
  For each hole found, identify its diameter dimension 
  and add it as a Family Table column.  
*/

package com.ptc.otkjavaexamples;


import com.ptc.cipjava.*;
import com.ptc.pfc.pfcGlobal.*;
import com.ptc.pfc.pfcSession.*;
import com.ptc.pfc.pfcCommand.*;

import com.ptc.pfc.pfcSolid.*;
import com.ptc.pfc.pfcFeature.*;
import com.ptc.pfc.pfcModelItem.*;
import com.ptc.pfc.pfcDimension.*;
import com.ptc.pfc.pfcFamily.*;
import com.ptc.pfc.pfcModel.*;

import com.ptc.wfc.wfcSession.*;
import com.ptc.wfc.wfcSolid.*;
import com.ptc.wfc.wfcFamily.*;


/* Family Member Example Class */
class otkFamilyMemberExample extends DefaultUICommandActionListener
{
    
    public otkFamilyMemberExample() 
    {
    } 
    
    public void OnCommand () 
    {
	addFamilyMemberExample();
    }
    
    /* Function to execute example code on current model */
    public static void addFamilyMemberExample()
    {
	
	try
	    {
		Session pSession = pfcSession.GetCurrentSession();
		WSession session = (WSession)pSession;
		
		Model model = session.GetCurrentModel ();		  		  
		if ((model == null) || 
		    ((model.GetType () != ModelType.MDL_ASSEMBLY) && 
		     (model.GetType () != ModelType.MDL_PART)))
		    {
			return;
		    }
		
		Solid solid = (Solid)model;			
		addHoleDiameterColumns(solid);		
		
	    }
	catch (Throwable e)
	    {
		System.out.println ("Exception caught: "+e);
		e.printStackTrace ();
	    }
    }
    
    
    /* Function to add diameter dimension of hole features as family table columns */  
    
    public static void addHoleDiameterColumns (Solid solid)
    {    
	Session session;
	Features hole_features;   
	Feature hole_feat;        
	ModelItems dimensions;    
	Dimension dim;            
	FamColDimension dim_column; 
	
	try 
	    {
		session = pfcSession.GetCurrentSession();
            }
	catch (jxthrowable x)     
	    {
		System.out.println("Unable to initialize Pro/E session: "+x);
		return;
	    }
	
	
	try {
	    /* List all holes in the solid model */
	    hole_features = solid.ListFeaturesByType (Boolean.TRUE, FeatureType.FEATTYPE_HOLE);
	    
	    WSolid wSolid = (WSolid)solid;	  
	    
	    /*Check family table validity before modification*/	  
	    FamilyTableStatus ft_status = wSolid.GetFamilyTableStatus();
	    System.out.println("\n----- FT Status Before -----");		  
	    printFamilyTableStatus(ft_status);
	    
	    /*Check if family table is modifiable*/
	    System.out.println("\nThis Solid's Family Table is: " +(wSolid.IsModifiable(false) ? "MODIFIABLE" : "NOT MODIFIABLE"));		  
	    
	    for (int ii =0; ii<hole_features.getarraysize(); ii++)
		{
		    hole_feat = hole_features.get(ii);
		    /* List all dimensions in the feature */
		    dimensions = hole_feat.ListSubItems(ModelItemType.ITEM_DIMENSION);
		    
		    for (int jj = 0; jj<dimensions.getarraysize(); jj++)
			{
			    dim = (Dimension) dimensions.get(jj);
			    /* Determine if the dimension is a diameter dimension. */
			    if (dim.GetDimType().equals(DimensionType.DIM_DIAMETER)) 
				{
				    /* Create the family table column */
				    
				    dim_column = solid.CreateDimensionColumn(dim);
				    
				    /* Add the column to the Solid
				       instead of null, you could pass any array of ParamValues 
				       for the initial column values */
				    
				    solid.AddColumn(dim_column, null);
				    
				}		  
			}
		    
		} 
	    
	    /*Check family table validity after modification*/	  
	    ft_status = wSolid.GetFamilyTableStatus();
	    System.out.println("\n----- FT Status After -----");		  
	    printFamilyTableStatus(ft_status);
	    
	    
	    
	}
	catch (jxthrowable x)
	    {
		System.out.println("Exception caught: "+x);
		return;
	    }
	
	
    }
    
    
    /* Function to print family table validity status */  
    
    public static void printFamilyTableStatus (FamilyTableStatus inStatus)
    {	
	
	if (FamilyTableStatus._FAMILY_TABLE_NONE == inStatus.getValue())
	    {
		System.out.println("FT Status: NO FAMILY TABLE"  );
	    }
	
	else if (FamilyTableStatus._FAMILY_TABLE_VALID_AND_NOT_EMPTY == inStatus.getValue())
	    {
		System.out.println("FT Status: FAMILY TABLE is VALID AND NOT EMPTY"  );
	    }
	
	else if (FamilyTableStatus._FAMILY_TABLE_VALID_AND_EMPTY == inStatus.getValue())
	    {
		System.out.println("FT Status: FAMILY TABLE is VALID AND EMPTY"  );
	    }
	
	else
	    {
		System.out.println("FT Status: INVALID STATUS"  );
	    }
	
    }
    
    
}
